package com.cn.wanxi.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.entity.UserEntity;
import com.cn.wanxi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class LoginController {

    @RequestMapping(value = "/loginHtml")
    public String loginHtml() {
        System.out.println("loginHtml");
        return "login";
    }

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/denglu")
    @ResponseBody
    public void login(UserEntity userEntity, HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String sessionCode = (String) req.getSession().getAttribute("code");
        userEntity.setSessionCode(sessionCode);
        String result = userService.login(userEntity);
        if ("success".equals(result)) {
            req.getSession().setAttribute(userEntity.getUsername(), userEntity.getUsername());
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        resp.getWriter().println(jsonObject);

    }
}
